package cn.smart.epic.core.aop;

import cn.smart.epic.common.annotion.log.BussinessLog;
import cn.smart.epic.common.constant.dictmap.base.AbstractDictMap;
import cn.smart.epic.common.constant.dictmap.factory.DictMapFactory;
import cn.smart.epic.core.log.LogManager;
import cn.smart.epic.core.log.LogObjectHolder;
import cn.smart.epic.core.log.factory.LogTaskFactory;
import cn.smart.epic.core.support.HttpKit;
import cn.smart.epic.core.util.Contrast;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Map;

/**
 * @Auther Uncle_Z
 * @date 2017/9/25 22:37
 * @Email:2545375@qq.com
 * @Descrition
 * @since 1.0
 */
@Aspect
@Component
public class LogAop {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Pointcut(value = "@annotation(cn.smart.epic.common.annotion.log.BussinessLog)")
    public void cutService() {
    }

    @Around("cutService()")
    public Object recordSysLog(ProceedingJoinPoint point) throws Throwable {
        //先执行业务
        Object result = point.proceed();
        try {
            handle(point);
        } catch (Exception e) {
            logger.error("日志记录出错", e);
        }
        return result;
    }

    private void handle(ProceedingJoinPoint point) throws Exception {
        Signature sig = point.getSignature();
        MethodSignature msig = null;
        if (!(sig instanceof MethodSignature)) {
            throw new IllegalArgumentException();
        }
        msig = (MethodSignature) sig;
        Object target = point.getTarget();
        Method currentMethod = target.getClass().getMethod(msig.getName(), msig.getParameterTypes());
        String methodName = currentMethod.getName();

        //获取拦截方法的参数;
        String className = target.getClass().getName();
        Object[] params = point.getArgs();

        //获取操作名称
        BussinessLog annot = currentMethod.getAnnotation(BussinessLog.class);
        String bussinessName = annot.value();
        String key = annot.key();
        String dict = annot.dict();

        StringBuilder sb = new StringBuilder();
        for (Object param : params) {
            sb.append(param);
            sb.append("&");
        }

        //如果涉及修改,对比变化
        String msg = "";
        if (bussinessName.indexOf("修改") != -1 || bussinessName.indexOf("编辑") != -1) {
            Object obj1 = LogObjectHolder.me().getObject();
            Map<String, String> obj2 = HttpKit.getRequestParameters();
            msg = Contrast.contrastObj(dict, key, obj1, obj2);
        }else{
            Map<String,String> requests = HttpKit.getRequestParameters();
            AbstractDictMap dictMap = DictMapFactory.createDictMap(dict);
            msg = Contrast.parseMutiKey(dictMap, key, requests);
        }
        LogManager.me().executeLog(LogTaskFactory.bussinessLog(1, bussinessName, className, methodName, msg));
    }
}
